package com.ktjy.crm;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.ktjy.crm.entity.User;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
public class RedisTemplateTester {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testString() {
        stringRedisTemplate.opsForValue().set("name", "czkt");
        assertEquals("czkt", stringRedisTemplate.opsForValue().get("name"));
    }

    @Test
    public void testObj(){
        User user=new User("czkt","123456",1L,null);
        ValueOperations<String,User> operations=redisTemplate.opsForValue();
        operations.set("ktjy.crm.user.czkt",user);
        User u=operations.get("ktjy.crm.user.czkt");
        System.out.println("user.userName："+u.getUsrName());
    }

    @Test
    public void testExpire() throws InterruptedException{
        User user=new User("czkt","123456",1L,null);
        ValueOperations<String,User> operations=redisTemplate.opsForValue();
        operations.set("expire",user,100, TimeUnit.MILLISECONDS);
        Thread.sleep(1000);
        boolean exists=redisTemplate.hasKey("expire");
        if (exists) {
            System.out.println("exists is true");
        }else {
            System.out.println("exists id false");
        }
    }

    @Test
    public void  testHash(){
        HashOperations<String,Object,Object> hash=redisTemplate.opsForHash();
        hash.put("hash","name","ktjiaoyu");
        String value= (String) hash.get("hash","name");
        System.out.println("hash name‘value:"+value);
    }

    @Test
    public void testList(){
        ListOperations<String,String> list=redisTemplate.opsForList();
        list.leftPush("list","accp");
        list.leftPush("list","bdpn");
        list.leftPush("list","czkt");
        String value=list.leftPop("list");
        System.out.println("list value:"+value.toString());
    }
}
